package com.fire.social.mapper;

import com.fire.social.entity.node.User;
import com.fire.social.entity.relationship.Follow;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface FollowMapper extends Neo4jRepository<Follow, Long> {

    /**
     * 查询被关注总数
     */
    @Query("match (me:User) where me.account=$account\n" +
            "match ((me)<-[:Follow]-(follower)) return count(follower)")
    Long countFollowMe(@Param("account")String account);

    /**
     * 查询关注总数
     */
    @Query("match (me:User) where me.account= $account\n" +
            "match ((me)-[:Follow]->(following)) return count(following)")
    Long countIFollow(@Param("account") String account);

    /**
     * 关注
     */
    @Query("MATCH (a:User),(b:User) WHERE a.account=$account AND b.account=$follower " +
            "CREATE (a)-[follow:Follow]->(b) RETURN ID(follow)")
    Long createFollow(@Param("account") String account, @Param("follower") String follower);

    /**
     * 取消关注
     */
    @Query("match (user:User) where user.account=$account\n" +
            "match (user2:User) where user2.account=$follower\n" +
            "match (user)-[r:Follow]->(user2) delete r return count(r)")
    Long deleteFollow(@Param("account") String account, @Param("follower") String follower);

    /**
     * 关注我的人
     */
    @Query("match (me:User) where me.account=$account\n" +
            "match ((me)<-[:Follow]-(follower)) return follower")
    List<User> getFollowMe(@Param("account")String account);

}
